package com.ydj.app.service;

import com.ydj.app.entity.Banner;
import com.ydj.app.entity.CmsAccount;
import com.ydj.app.entity.WebUser;
import com.ydj.app.entity.dao.BannerDao;
import com.ydj.app.entity.query.BannerQuery;
import com.ydj.app.enumerate.MemberLevelEnum;
import com.ydj.app.protocol.http.cms.*;
import com.ydj.app.protocol.http.web.WebBannerListRes;
import com.ydj.framework.exception.CommonException;
import com.ydj.framework.mysql.PageArgs;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * banner
 */
@Slf4j
@Service
public class BannerService {
    @Autowired
    BannerDao bannerDao;

    /**
     * 列表
     */
    public CmsBannerListRes cmsBannerList(CmsBannerListReq req) {
        PageArgs pageArgs = req.getPageArgs();

        List<Banner> list = new BannerQuery(bannerDao)
                .memberLevelContains(req.getMemberLevel())
                .enable(req.getEnable())
                .orderByOrderNumberDesc()
                .page(pageArgs);

        CmsBannerListRes res = new CmsBannerListRes(pageArgs);
        for (Banner banner : list) {
            CmsBannerListRes.CmsBannerListResItem item = new CmsBannerListRes.CmsBannerListResItem()
                    .setId(banner.getId())
                    .setTitle(banner.getTitle())
                    .setImage(banner.getImage())
                    .setOrderNumber(banner.getOrderNumber())
                    .setAction(banner.getAction())
                    .setActionUrl(banner.getActionUrl())
                    .setMemberLevelList(banner.getMemberLevelList())
                    .setEnable(banner.getEnable())
                    .setRemark(banner.getRemark());
            res.getList().add(item);
        }

        return res;
    }

    /**
     * 新增
     */
    public void cmsBannerCreate(CmsBannerCreateReq req) {
        // 找出最大的排序号
        Banner maxOrderNumberBanner = new BannerQuery(bannerDao)
                .orderByOrderNumberAsc()
                .limit(1)
                .single();
        int maxOrderNumber = maxOrderNumberBanner == null ? 0 : maxOrderNumberBanner.getOrderNumber() + 1;

        // 新建
        Banner banner = new Banner()
                .setTitle(req.getTitle())
                .setImage(req.getImage())
                .setOrderNumber(maxOrderNumber)
                .setAction(req.getAction())
                .setActionUrl(req.getActionUrl())
                .setMemberLevelList(req.getMemberLevelList())
                .setEnable(req.getEnable())
                .setRemark(req.getRemark());

        bannerDao.saveOrUpdate(banner);
    }

    /**
     * 修改
     */
    public void cmsBannerUpdate(CmsBannerUpdateReq req) throws CommonException {
        Banner banner = new BannerQuery(bannerDao)
                .id(req.getId())
                .single();
        CommonException.throwExceptionIfNull(banner, "未找到对应的Banner");

        banner.setTitle(req.getTitle())
                .setImage(req.getImage())
                .setOrderNumber(req.getOrderNumber())
                .setAction(req.getAction())
                .setMemberLevelList(req.getMemberLevelList())
                .setEnable(req.getEnable())
                .setRemark(req.getRemark());
        switch (req.getAction()) {
            case 不跳转:
                banner.setActionUrl(null);
                break;
            case 跳转至链接:
                banner.setActionUrl(req.getActionUrl());
                break;
            default:
                CommonException.throwException("不支持的Banner动作");
        }

        bannerDao.updateById(banner);
    }

    /**
     * 删除
     */
    public void cmsBannerDelete(CmsBannerDeleteReq req) throws CommonException {
        Banner banner = new BannerQuery(bannerDao)
                .id(req.getId())
                .single();
        CommonException.throwExceptionIfNull(banner, "未找到对应的Banner");

        bannerDao.removeById(req.getId());
    }

    /**
     * CMS-调整轮播图顺序
     */
    public void cmsBannerAdjustOrder(CmsBannerAdjustOrderReq req, CmsAccount account) {
        List<Long> idList = req.getList().stream()
                .map(item -> item.getId())
                .collect(Collectors.toList());
        Map<Long, Banner> map = new BannerQuery(bannerDao)
                .ids(idList)
                .map();
        for (CmsBannerAdjustOrderReq.CmsBannerAdjustOrderReqItem item : req.getList()) {
            Banner banner = map.get(item.getId());
            if (banner == null) {
                continue;
            }
            banner.setOrderNumber(item.getOrderNumber());
        }
        bannerDao.saveOrUpdateBatch(map.values());
    }

    /**
     * 首页，获取banner轮播
     */
    public WebBannerListRes webList(WebUser webUser) {
        BannerQuery query = new BannerQuery(bannerDao)
                .enable(true)
                .orderByOrderNumberDesc();
        if (webUser == null) {
            query.memberLevelContains(MemberLevelEnum.非会员);
        } else {
            query.memberLevelContains(webUser.getLevel());
        }
        List<Banner> list = query.list();

        WebBannerListRes res = new WebBannerListRes();
        for (Banner banner : list) {
            WebBannerListRes.WebBannerListResItem item = new WebBannerListRes.WebBannerListResItem()
                    .setId(banner.getId())
                    .setTitle(banner.getTitle())
                    .setImage(banner.getImage())
                    .setAction(banner.getAction())
                    .setActionUrl(banner.getActionUrl());
            res.getList().add(item);
        }
        return res;
    }
}